﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.U2D;

namespace BF
{
    public class AtlasManager
    {
        /// <summary>
        /// Sprite Atalas管理器
        /// </summary>
        private static Dictionary<string, SpriteAtlas> _atlasDic = new Dictionary<string, SpriteAtlas>();
        private static Dictionary<string, Sprite> _spriteDic = new Dictionary<string, Sprite>();

        #region 图集相关

        /// <summary>
        /// 从Sprite Atalas中获取一个Sprite
        /// </summary>
        /// <param name="path">SpriteAtalas在Resources/SpriteAtlas/文件夹下的路径，比如如果在atlas/xxx.spriteatlas，那么就应该传atlas/xxx</param>
        /// <param name="sprName">图片名字</param>
        /// <returns></returns>
        public static Sprite GetSprite(string path, string sprName)
        {
            try
            {
                path = "SpriteAtlas/" + path;
                //解决GetSprite采用clone方式，可能会引起引用计数一直不清除的泄露
                if (_spriteDic.ContainsKey(path + sprName))
                {
                    return _spriteDic[path + sprName];
                }
                else
                {
                    if (!_atlasDic.ContainsKey(path))
                    {
                        LoadAtlas(path);
                    }
                    Sprite spt = _atlasDic[path].GetSprite(sprName);
                    _spriteDic.Add(path + sprName, spt);
                    return spt;
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"[TPAtlas] GetError {path}, {sprName}, {e.Message}");
            }
            return null;
        }


        /// <summary>
        /// 加载一个Sprite Atalas
        /// </summary>
        /// <param name="path">Sprite Atalas在Resources文件夹下的路径，比如如果在atlas/xxx.spriteatlas，那么就应该传atlas/xxx</param>
        /// <returns></returns>
        public static void LoadAtlas(string path)
        {
            if (_atlasDic.ContainsKey(path))
            {
                return;
            }
            SpriteAtlas atlas = Resources.Load<SpriteAtlas>(path);
            _atlasDic.Add(path, atlas);
        }

        /// <summary>
        /// 卸载一个Sprite Atalas
        /// </summary>
        /// <param name="path"></param>
        public static void UnloadAtlas(string path)
        {
            if (_atlasDic.ContainsKey(path))
            {
                SpriteAtlas atlas = _atlasDic[path];
                _atlasDic.Remove(path);
                Resources.UnloadAsset(atlas);
            }
            else
            {
                Debug.LogError("can not find " + path + " to be unload");
            }
        }

        /// <summary>
        /// 卸载所有纹理集
        /// </summary>
        public static void UnloadAllAtlas()
        {
            foreach (var pair in _atlasDic)
            {
                Resources.UnloadAsset(pair.Value);
            }
            _atlasDic.Clear();

            _spriteDic.Clear();
        }

        /// <summary>
        /// 卸载所有纹理集
        /// </summary>
        public static IEnumerator AsyncUnloadAllAtlas()
        {
            List<string> keys = new List<string>(_atlasDic.Keys);
            foreach (string key in keys)
            {
                if (!_atlasDic.ContainsKey(key))
                {
                    continue;
                }
                Resources.UnloadAsset(_atlasDic[key]);
                _atlasDic.Remove(key);
                _spriteDic.Remove(key);
                yield return null;
            }
        }
        #endregion
    }
}